//
//  XHCheckInViewController.m
//  umibus
//
//  Created by fengyn on 16/6/20.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHCheckInViewController.h"

#import "Services.h"

@interface XHCheckInViewController ()
@property (weak, nonatomic) IBOutlet UILabel *ui_titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *ui_valueLabel1;
@property (weak, nonatomic) IBOutlet UILabel *ui_valueLabel2;
@property (weak, nonatomic) IBOutlet UILabel *ui_valueLabel3;
@property (weak, nonatomic) IBOutlet UILabel *ui_valueLabel4;
@property (weak, nonatomic) IBOutlet UILabel *ui_detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *ui_noteLabel;
@property (strong, nonatomic) NSDictionary * m_userDic;
@property (strong, nonatomic) NSDictionary * m_checkDic;
@property (strong, nonatomic) NSArray * m_scoreInCouponArray;
@property (weak, nonatomic) IBOutlet UIButton *ui_checkInButton;
@property (weak, nonatomic) IBOutlet UIView *ui_seccesView;
@property (weak, nonatomic) IBOutlet UIImageView *ui_xingImageView;
@property (weak, nonatomic) IBOutlet UIImageView *ui_qiuImageView;
@property (weak, nonatomic) IBOutlet UILabel *ui_seccessLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_xingViewConstraintHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_xingViewConstraintWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_qiuViewConstraintWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_qiuViewConstraintHeight;
@property (weak, nonatomic) IBOutlet UILabel *ui_promptLabel;

@end

@implementation XHCheckInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"签到";
    self.ui_noteLabel.text=@"";
    self.ui_seccessLabel.text=@"签到\n成功";
    if ([UIScreen mainScreen].bounds.size.width>320) {
        self.ui_noteLabel.font=[UIFont systemFontOfSize:17];
    }
    self.ui_valueLabel1.layer.cornerRadius=3;
    self.ui_valueLabel2.layer.cornerRadius=3;
    self.ui_valueLabel3.layer.cornerRadius=3;
    self.ui_valueLabel4.layer.cornerRadius=3;
    self.ui_valueLabel1.layer.masksToBounds=YES;
    self.ui_valueLabel2.layer.masksToBounds=YES;
    self.ui_valueLabel3.layer.masksToBounds=YES;
    self.ui_valueLabel4.layer.masksToBounds=YES;
    
    self.ui_checkInButton.layer.masksToBounds=YES;
    self.ui_checkInButton.layer.cornerRadius=33/2;
    [self.ui_checkInButton setBackgroundColor:[ColorManager colorWithHexString:@"#FFE007"] forState:UIControlStateNormal];
    [self.ui_checkInButton setBackgroundColor:[ColorManager colorWithHexString:@"#B0BBBE"] forState:UIControlStateDisabled];
    [self.ui_checkInButton setTitleColor:[ColorManager colorWithHexString:@"#6B512B"] forState:UIControlStateNormal];
    [self.ui_checkInButton setTitleColor:[ColorManager colorWithHexString:@"#FFFFFF"] forState:UIControlStateDisabled];
    [self.ui_checkInButton setTitle:@"已签到" forState:UIControlStateDisabled];
    self.ui_seccesView.hidden=YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getUserInfo];
    [self loadScoreInCouponData];
    [self updateView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)loadIsSignData{
    [ServiceManager getIsSignTodaySuccess:^(NSDictionary *data) {
        if([data isKindOfClass:[NSString class]]){
            if ([(NSString *)data isEqualToString:@"Y"]) {
                self.m_isSignToday=YES;
            }else{
                self.m_isSignToday=NO;
            }
            [self updateView];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)loadScoreInCouponData{
    [ServiceManager getScoreInCouponSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSArray class]]) {
            self.m_scoreInCouponArray=(NSArray *)data;
            [self updateView];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (IBAction)seccessViewTouchAction:(id)sender {
    self.ui_seccesView.hidden=YES;
}

- (IBAction)checkInButtonAction:(UIButton *)sender {
    [self checkInData];
}

- (void)showSeccesViewAnimation{
    self.ui_seccesView.hidden=NO;
    self.la_xingViewConstraintWidth.constant=40;
    self.la_xingViewConstraintHeight.constant=40;
    self.la_qiuViewConstraintWidth.constant=1;
    self.la_qiuViewConstraintHeight.constant=1;
    self.ui_seccessLabel.hidden=YES;
    [self.ui_seccesView layoutIfNeeded];
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.la_xingViewConstraintWidth.constant=239;
        self.la_xingViewConstraintHeight.constant=166;
        [self.ui_seccesView layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
    
    [UIView animateWithDuration:0.35 delay:0.2 usingSpringWithDamping:0.3 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.la_qiuViewConstraintWidth.constant=111;
        self.la_qiuViewConstraintHeight.constant=108;
        [self.ui_seccesView layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.ui_seccessLabel.hidden=NO;
    }];
}

- (void)checkInData{
    [ServiceManager signInSuccess:^(NSDictionary *data) {
        self.m_checkDic=data;
        self.m_isSignToday=YES;
        [self updateView];
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString *message=[NSString stringWithFormat:@"恭喜你获得%@积分!",data[@"signScore"]];
            NSDictionary *couponModel=data[@"couponModel"];
            if ([couponModel isKindOfClass:[NSDictionary class]]) {
                message=[NSString stringWithFormat:@"恭喜你获得%@积分!\n并获得%@%@%@",data[@"signScore"],couponModel[@"operatorName"],couponModel[@"suitableTypeName"],couponModel[@"couponName"]];
            }
//            [PromptAction showSuccessPrompt:message];
            self.ui_promptLabel.text=message;
            [self showSeccesViewAnimation];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)getUserInfo
{
    WS(weakSelf)
    self.m_userDic=[ServiceManager getUserInfoSuccess:^(NSDictionary *data) {
        weakSelf.m_userDic = data;
        [self updateView];
    } failure:^{
        
    }];
    [self updateView];
}

- (void)updateView{
    NSString *typeString=@"连续";
    if (self.m_userDic) {
        if ([self.m_userDic[@"signInWay"] isEqualToString:@"ADD_UP"]) {
            typeString=@"累计";
        }else if ([self.m_userDic[@"signInWay"] isEqualToString:@"CONTINUOUS"]){
            typeString=@"连续";
        }
    }
    
    self.ui_checkInButton.enabled=!self.m_isSignToday;
    
    if (self.m_checkDic) {
//        self.ui_titleLabel.text=@"签到成功";
        int dayCount=[self.m_checkDic[@"signValue"] intValue];
        if ([self.m_checkDic[@"signInWay"] isEqualToString:@"ADD_UP"]) {
            dayCount=[self.m_checkDic[@"signValue"] intValue];
        }else if ([self.m_userDic[@"signInWay"] isEqualToString:@"CONTINUOUS"]){
            dayCount=[self.m_checkDic[@"signValue"] intValue];
        }
        self.ui_titleLabel.text=[NSString stringWithFormat:@"已%@签到天数",typeString];
        int day1=dayCount/1000;
        int day2=(dayCount%1000)/100;
        int day3=((dayCount%1000)%100)/10;
        int day4=((dayCount%1000)%100)%10;
        self.ui_valueLabel1.text=[NSString stringWithFormat:@"%d",day1];
        self.ui_valueLabel2.text=[NSString stringWithFormat:@"%d",day2];
        self.ui_valueLabel3.text=[NSString stringWithFormat:@"%d",day3];
        self.ui_valueLabel4.text=[NSString stringWithFormat:@"%d",day4];
    }else if(self.m_userDic){
//        self.ui_titleLabel.text=@"已签到";
        self.ui_titleLabel.text=[NSString stringWithFormat:@"已%@签到天数",typeString];
        int dayCount=[self.m_userDic[@"signValue"] intValue];
        int day1=dayCount/1000;
        int day2=(dayCount%1000)/100;
        int day3=((dayCount%1000)%100)/10;
        int day4=((dayCount%1000)%100)%10;
        self.ui_valueLabel1.text=[NSString stringWithFormat:@"%d",day1];
        self.ui_valueLabel2.text=[NSString stringWithFormat:@"%d",day2];
        self.ui_valueLabel3.text=[NSString stringWithFormat:@"%d",day3];
        self.ui_valueLabel4.text=[NSString stringWithFormat:@"%d",day4];
    }
    
    if (self.m_scoreInCouponArray) {
        NSMutableString *noteString=[NSMutableString stringWithString:@""];
        for (NSDictionary *dic in self.m_scoreInCouponArray) {
            NSDictionary *couponTypeModel=dic[@"couponTypeModel"];
            if ([couponTypeModel isKindOfClass:[NSDictionary class]]) {
                [noteString appendFormat:@"* %@签到%@天,送%@%@%@\n",typeString,dic[@"accumulateDays"],couponTypeModel[@"operatorCodeName"],couponTypeModel[@"suitableTypeName"],couponTypeModel[@"couponName"]];
            }
        }
        self.ui_noteLabel.text=noteString;
    }
}
@end
